{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import scipy\n",
    "\n",
    "import plotly\n",
    "\n",
    "# 使用离线\n",
    "import plotly.offline as py\n",
    "# import plotly.plotly as py\n",
    "import plotly.graph_objs as go\n",
    "\n",
    "# from plotly.tools import FigureFactory as FF\n",
    "import plotly.figure_factory as FF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 离线绘图\n",
    "py.init_notebook_mode(connected=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv(\"./2010_alcohol_consumption_by_country.csv\")\n",
    "print(type(data), data.size)\n",
    "\n",
    "df = data[0:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "table = FF.create_table(df)\n",
    "# plot: 绘制的图存到html, iplot直接显示到inotebook上\n",
    "py.iplot(table, filename='hello-test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mu1 = np.mean(data['alcohol'])\n",
    "mu2 = np.mean(data.alcohol)\n",
    "\n",
    "sigma1 = np.std(data.alcohol)\n",
    "\n",
    "print(\"the alcohol mean: %r vs %r, std: %r\" % (mu1, mu2, sigma1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pandas.core.series.Series, numpy.ndarray, list\n",
    "print(\"type: data.alcohol(%s), alcohol.values(%s), values.tolist(%s)\" %(\n",
    "    type(data.alcohol),\n",
    "    type(data.alcohol.values),\n",
    "    type(data.alcohol.values.tolist())))\n",
    "\n",
    "print(\"\\n\\n\", data.alcohol[0:5], \"\\n\\n\", data.alcohol.values[0:5], \"\\n\\n\", data.alcohol.values.tolist()[0:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = FF.create_violin(data=data.alcohol.values, title=\"Test-Violin\", colors=\"#00EEEE\")\n",
    "# 小提琴图: 用于显示数据的分布及它的概率密度\n",
    "# 中间白点:均值, 粗黑线: 4分位, 细黑线: 95%置信区间, 宽度: 频度\n",
    "py.iplot(figure_or_data=fig, filename=\"test-violin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 箱线图: 最大值, 最小值, 中位数, 四分位数, 异常值\n",
    "data_box = [\n",
    "    go.Box(\n",
    "        y=data.alcohol.values\n",
    "    )\n",
    "]\n",
    "# plotly.offline.iplot(data_box)\n",
    "\n",
    "trace0 = go.Box(\n",
    "    y=data.alcohol.values,\n",
    "    name='Box Plot',\n",
    "    boxpoints='all',\n",
    "    jitter=0.3,\n",
    "    marker=dict(\n",
    "        color='rgb(214,12,140)'\n",
    "    )\n",
    ")\n",
    "\n",
    "layout = go.Layout(\n",
    "    width=500,\n",
    "    yaxis=dict(\n",
    "        title='Alcohol Consumption by Country',\n",
    "        zeroline=False\n",
    "    ),\n",
    ")\n",
    "fig = go.Figure(data=[trace0], layout=layout)\n",
    "plotly.offline.iplot(fig, filename=\"Test-Box\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
